import pandas as pd
df = pd.read_excel("./datas.xlsx")
datas = df.iloc[:,29].tolist()

import jieba
words = jieba.lcut(str(datas))
counts = {}     # 通过键值对的形式存储词语及其出现的次数
for word in words:
    if  len(word) == 1:    # 单个词语不计算在内
        continue
    else:
        counts[word] = counts.get(word, 0) + 1    # 遍历所有词语，每出现一次其对应的值加 1       
items = list(counts.items())#将键值对转换成列表
items.sort(key=lambda x: x[1], reverse=True)    # 根据词语出现的次数进行从大到小排序
# for i in range(60):
#     word, count = items[i]
#     print("{0:<5}{1:>5}".format(word, count))

import json
from pyecharts import options as opts
from pyecharts.charts import WordCloud
# with open("wordcloud_image_base64.json", "r", encoding="utf-8") as f:
#     j = json.load(f)
# c = (
#     WordCloud()
#     .add("", words, word_size_range=[12, 55], mask_image=j["base64"])
#     .set_global_opts(title_opts=opts.TitleOpts(title="WordCloud-自定义图片"))
#     .render("wordcloud_custom_mask_image.html")
# )

# (
#     WordCloud()
#     .add(series_name="热点分析", data_pair=items, word_size_range=[6, 66])
#     .set_global_opts(
#         title_opts=opts.TitleOpts(
#             title="热点分析", title_textstyle_opts=opts.TextStyleOpts(font_size=23)
#         ),
#         tooltip_opts=opts.TooltipOpts(is_show=True),
#     )
#     .render("basic_wordcloud.html")
# )

# c = (
#     WordCloud()
#     .add(
#         "",
#         items,
#         word_size_range=[20, 100],
#         textstyle_opts=opts.TextStyleOpts(font_family="cursive"),
#     )
#     .set_global_opts(title_opts=opts.TitleOpts(title="WordCloud-自定义文字样式"))
#     .render("wordcloud_custom_font_style.html")
# )

# from pyecharts.globals import SymbolType
# w = (
#     WordCloud()
#     .add("", items,
#     shape=SymbolType.RECT)
#     .set_global_opts(title_opts=opts.TitleOpts(title="WordCloud-旅游"))
#     .render("wordcloud_custom_mask_image.html")
# )
# with open("wordcloud_image_base64.json", "r", encoding="utf-8") as f:
#     j = json.load(f)


# c = (
#     WordCloud()
#     .add("", items, word_size_range=[12, 55],
#     # mask_image=j["base64"]
#     )
#     .set_global_opts(title_opts=opts.TitleOpts(title="WordCloud-自定义图片"))
#     .render("wordcloud_custom_mask_image.html")
# )

# wc = WordCloud()
# wc.add(series_name="双减", data_pair=items[:100])
# wc.render("wordcloud.html")
wc = WordCloud()
wc.add(series_name="双减", data_pair=items[1:150])
wc.render("wordcloud1.html")